//
//  AY_SearchRecommonCell.m
//  AiYue
//
//  Created by mac on 2023/11/9.
//

#import "AY_SearchRecommonCell.h"
#import "AY_SearchRecommChildCell.h"
#import "AY_BookDetailViewController.h"
@interface AY_SearchRecommonCell ()<UITableViewDataSource,UITableViewDelegate>

/**
 UITableView 基类
 */
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic, strong) UIImageView *topImg;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *info;

@end

@implementation AY_SearchRecommonCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.contentView.backgroundColor = COLOR(@"F7F5F6");
        self.backView = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(8) andAddSubView:self.contentView];
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView).offset(-YNUM(16));
        }];
        
        self.topImg = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_search_top") andBg:nil andRadius:0 andAddSubView:self.backView];
        [self.topImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self.backView);
            make.height.mas_equalTo(YNUM(46));
        }];
        
        self.info = [AYTool createLabelWithFrame:CGRectZero andText:@"热搜榜单" andTextColor:THEME_COLOR andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(16)] andLines:1 andRadius:0 andAddSubView:self.topImg];
        [self.info mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.topImg).offset(YNUM(16));
            make.centerY.equalTo(self.topImg);
        }];
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.dataSource = self;
        self.tableView.scrollEnabled = NO;
        self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.delegate = self;
        self.tableView.tableFooterView = [[UIView alloc] init];
        [self.backView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.backView);
            make.top.equalTo(self.topImg.mas_bottom);
        }];
    }
    return self;
}

- (void)setRecommends:(NSMutableArray *)recommends {
    _recommends = recommends;
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommends.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"AY_SearchRecommChildCell";
    AY_SearchRecommChildCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[AY_SearchRecommChildCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SearchModel *model = self.recommends[indexPath.row];
    model.index = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YNUM(68);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
    if(self.recommends.count > 0) {
        SearchModel *model = self.recommends[indexPath.row];
        bookDetailVC.bookId = model.bookId;
    }
    [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
